import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login'
const Login = () => import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Login.vue')
// import Home from '../components/Home'
const Home = () => import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome'
const Welcome = () => import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Welcome.vue')
// import Users from '../components/user/Users'
const Users = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/user/Users.vue')
// import Rights from '../components/power/Rights'
const Rights = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Rights.vue')
// import Roles from '../components/power/Roles'
const Roles = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Roles.vue')
// import Categories from '../components/goods/Categories'
const Categories = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Categories')
// import Params from '../components/goods/Params'
const Params = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Params.vue')
// import Goods from '../components/goods/Goods'
const Goods = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Goods.vue')
// import Add from '../components/goods/Add'
const Add = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Add.vue')
const Edit = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Edit.vue')
// import Orders from '../components/orders/Orders'
const Orders = () => import(/* webpackChunkName:"Orders" */ '../components/orders/Orders.vue')
// import Reports from '../components/reports/Reports'
const Reports = () => import(/* webpackChunkName:"Reports" */ '../components/reports/Reports.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: Goods

      },
      {
        path: '/goods/add',
        component: Add

      },
      {
        path: '/goods/edit',
        component: Edit

      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  },
]



const router = new VueRouter({
  routes
})

// 配置路由守卫
router.beforeEach((to, from, next) => {
  // 如果访问登录页，则放行
  if (to.path === '/login') {
    return next();
  }
  // 如果用户未登录，则跳转到/login
  const token = sessionStorage.getItem('token');
  if (!token) {
    return next('/login');
  }
  // 如果用户已登录，则放行
  return next();
})



export default router
